package com.jayway.opengl.tutorial;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PointF;
import android.graphics.RectF;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class TutorialPartVI extends Activity implements OnTouchListener {
	public enum PhotoCropMode {
		NONE, DRAG, ZOOM
	}

	PhotoCropMode mPhotoCropMode = PhotoCropMode.NONE;
	PointF mCropStartPoint = new PointF();
	PointF mCropMidPoint = new PointF();
	float mCropInitDistance;
	float mLastTranslate[] = new float[2];
	float mLastScale = 1.0f;

	GLSurfaceView view;
	OpenGLRenderer renderer;
	HighlightView highlightView;

	boolean toogle = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.main);

		view = (GLSurfaceView) findViewById(R.id.surfaceView);
		view.setEGLContextClientVersion(2);

		renderer = new OpenGLRenderer(this);
		view.setRenderer(renderer);
		view.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

		view.setOnTouchListener(this);

		highlightView = (HighlightView) findViewById(R.id.view_highlight);
		int width = getResources().getDisplayMetrics().widthPixels;
		highlightView.setHighlightHeight(width);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			System.out.println("action down");
			float scale = 2.0f;
			RectF rect = highlightView.getHightlightRect();
			 float midX = event.getX();
			 float midY = event.getY();
			//float midX = rect.left;
			//float midY = rect.top;
			midX -= (rect.left + rect.right) / 2.0f;
			midY -= (rect.top + rect.bottom) / 2.0f;
			
			//0, 0, scale=1.0
			//-360,-360, scale=2.0
			//-360*(1-2)/360,-360*(2-1)/576,scale=2.0
			
			//1, 0.5625, scale=2.0
			//-360,-360,scale=2.0
			//

			System.out.println("distance: midx = " + midX + "; midY = " + midY);

			midX -= mLastTranslate[0] * renderer.mWidth / 2.0f;
			midY += mLastTranslate[1] * renderer.mHeight / 2.0f;

			System.out.println("distance: midx = " + midX + "; midY = " + midY);

			float translateX = midX * (1.0f - scale);
			float translateY = midY * (scale - 1.0f);

			System.out.println("translateX = " + translateX + "; translateY = "
					+ translateY);

			renderer.mScale *= scale;
			renderer.mTranslate[0] = translateX * 2.0f / renderer.mWidth
					+ mLastTranslate[0];
			renderer.mTranslate[1] = translateY * 2.0f / renderer.mHeight
					+ mLastTranslate[1];

			System.out.println("translate[0] = " + renderer.mTranslate[0]);
			System.out.println("translate[1] = " + renderer.mTranslate[1]);

			view.requestRender();
			break;
		case MotionEvent.ACTION_UP:
			mLastTranslate[0] = renderer.mTranslate[0];
			mLastTranslate[1] = renderer.mTranslate[1];
			mLastScale = renderer.mScale;

			view.requestRender();
			break;
		default:
			break;
		}

		return true;
	}

	@SuppressLint("FloatMath")
	private float distance(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);

		return FloatMath.sqrt(x * x + y * y);
	}
}